package com.eirikb.client.ui;

import com.eirikb.client.Client;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.KeyboardListener;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 *
 * @author eirikb
 */
public class Login extends VerticalPanel {

    private TextBox nickBox;
    private TextBox nameBox;
    private Label info;
    private Client client;
    private Button submit;
    private boolean connected;

    public Login(Client client2) {
        this.client = client2;
        add(new Label("Nick:"));
        KeyboardListener listener = new KeyboardListener() {

            public void onKeyDown(Widget arg0, char arg1, int arg2) {
            }

            public void onKeyPress(Widget arg0, char arg1, int arg2) {
                if (arg1 == 13) {
                    submit.click();
                }
            }

            public void onKeyUp(Widget arg0, char arg1, int arg2) {
            }
        };
        nickBox = new TextBox();
        nickBox.setMaxLength(9);
        nickBox.addKeyboardListener(listener);
        add(nickBox);
        add(new Label("Navn:"));
        nameBox = new TextBox();
        nameBox.addKeyboardListener(listener);
        add(nameBox);
        submit = new Button("OK!");
        submit.addClickListener(new ClickListener() {

            public void onClick(Widget arg0) {
                nickBox.setText(nickBox.getText().replaceAll(" ", ""));
                if (nickBox.getText().length() > 0 && nameBox.getText().length() > 0) {
                    submit.setEnabled(false);
                    if (!connected) {
                        setInfo("Kobler til...");
                        connect();
                    } else {
                        setInfo("Bytter nick...");
                        changeNick();
                    }
                } else if (nickBox.getText().length() == 0) {
                    nickBox.setFocus(true);
                    setInfo("Nick kan ikke være tomt");
                } else if (nameBox.getText().length() == 0) {
                    nameBox.setFocus(true);
                    setInfo("Navn kan ikke være tomt");
                }
            }
        });
        add(submit);
        add(info = new Label(" "));
    }

    private void connect() {
        final AsyncCallback<Boolean> callback = new AsyncCallback<Boolean>() {

            public void onSuccess(Boolean result) {
                if (result) {
                    setInfo("Koblet til! Logger på...");
                    connected = true;
                    client.loop(true);
                } else {
                    client.loop(false);
                    setInfo("Kunne ikke koble til server?!");
                }
            }

            public void onFailure(Throwable caught) {
                Window.alert(caught.toString());
            }
        };
        client.setNick(nickBox.getText());
        Client.getService().login(nickBox.getText(), nameBox.getText(), callback);
    }

    public void setInfo(String infoText) {
        info.setText(infoText);
    }

    public void enableSumit() {
        submit.setEnabled(true);
    }

    private void changeNick() {
        Client.getService().changeNick(client.getNick(), nickBox.getText(), new AsyncCallback<Boolean>() {

            public void onSuccess(Boolean result) {
                if (result) {
                    client.setNick(nickBox.getText());
                    client.loop(true);
                } else {
                    client.loop(false);
                    setInfo("Nick finnes fra før! ");
                }
            }

            public void onFailure(Throwable caught) {
                Window.alert(caught.toString());
            }
        });

        client.loop(true);
    }
}
